﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using TaobaoDistrSales.Common;
using TaobaoDistrSales.Model;
using System.Threading;

namespace TaobaoDistrSales
{
    public partial class MainOperationForm : DockContent
    {
        #region Ctor

        public MainOperationForm()
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        public int TotalDashBoardCount
        {
            get;
            set;
        }

        private List<int> ProcessShopIDList
        {
            get;
            set;
        }
         
        #endregion

        #region Load

        private void MainOperationForm_Load(object sender, EventArgs e)
        { 
            ProcessShopIDList = new List<int>();

            try
            {
                taobaodbEntities entities = ResourceManager.Current.LoadEntity();
                OptionsConfig config = entities.OptionsConfig.FirstOrDefault();
                TotalDashBoardCount = config != null ? config.displaypanelnum : 1;


                MainSplitContainer.Panel2Collapsed = TotalDashBoardCount <= 1;

                // TODO: I only consider the only one panel in the dashboard.    
                //var shop = ResourceManager.Current.Entities.Shop.First();
                var shop = entities.Shop.FirstOrDefault();
                if (shop == null)
                {
                    MessageBox.Show("数据库里没有店铺信息，请先输入店铺信息后再运行程序。");
                    return;
                }

                TaobaoFeiXiaoProcessor processor = new TaobaoFeiXiaoProcessor(shop.ShopId);
                ProcessShopIDList.Add(shop.ShopId);
                processor.SwitchShop = sh =>
                {
                    var shopid=sh.ShopId;
                    try
                    {
                        taobaodbEntities entities0 = ResourceManager.Current.LoadEntity();
                        var shops = from s in entities0.Shop
                                    select s;
                        var s1 = shops.ToList().FirstOrDefault(sp =>
                        {
                            return !ProcessShopIDList.Contains(sp.ShopId);
                        });
                        shopid = s1 == null ? sh.ShopId : s1.ShopId;
                        ProcessShopIDList.Add(shopid);
                        return shopid;
                    }
                    catch { }
                    return shopid;
                };
                this.LeftTopDashBoardControl.TaobaoProcessor = processor;
            }
            catch(Exception error) {
                MessageBox.Show(error.Message);
            }
        }
        
        #endregion

        #region Processor Events
         
        #endregion
         
         
    }
}
